package com.hi.hiparty.location;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.hi.hiparty.util.HttpUtil;

public class BaiduMapService {

	private static final String TAG = BaiduMapService.class.getSimpleName();

	private static final String BAIDU_MAP_KEY = "a4012fcd580406fbbf1b8a34c79f6fa4";

	public static String getAddressByLocation(String lonlat) {
		String[] lonlats = lonlat.split(",");
		String mapapi = "http://api.map.baidu.com/geocoder?output=json&location=" + lonlats[0] + ",%20" + lonlats[1]
				+ "&key=" + BAIDU_MAP_KEY;
		String resultJSONValue = HttpUtil.GET(mapapi);
		if (null == resultJSONValue) {
			return null;
		}
		try {
			JSONObject addressJSON = new JSONObject(resultJSONValue);
			if ("OK".equals(addressJSON.getString("status"))) {
				return addressJSON.getJSONObject("result").getString("formatted_address");
			}
			Log.w(TAG, "请求Baidu地图API解析经纬度获取地址名时返回值错误:" + addressJSON.getString("status"));
			return null;
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e(TAG, "处理baidu地图请求时发生JSON异常");
			return null;
		}
	}
	
}
